package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.domain.Corporation;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.CorporationService;
import com.qiaofang.applet.service.other.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.modules.other.service.dto.CityCompanyQuery;
import me.zhengjie.modules.other.service.dto.SaveCityCompanyDto;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author QF2019
 */
@Api(tags = "内容：城市公司配置")
@RestController
@RequestMapping("/api/cityCompany")
public class CityCompanyController {

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private UserService userService;

    @Autowired
    private CorporationService corporationService;

    @GetMapping
    @ApiOperation(value = "查询城市", notes = "查询城市")
    public ResponseEntity<Object> query(CityCompanyQuery query, Pageable pageable){
        List<CityCompanyDto> contents = new ArrayList<>();
        if(StringUtils.isNotEmpty(query.getCorporationCode())) {
            contents = cityCompanyService.getCompanyList(query.getCorporationCode());
        }
        return ResponseEntity.ok(new PageImpl(contents, pageable, contents != null ? contents.size() : 0));
    }

    @PostMapping
    @ApiOperation(value = "新增城市", notes = "新增城市")
    public ResponseEntity<Object> create(@Validated @RequestBody CityCompany cityCompany) {
        if (StringUtils.isNotBlank(cityCompany.getSubWebDomain())){
            Boolean existed = cityCompanyService.subWebDomainIsOnly(cityCompany);
            if (existed){
                throw new BusinessException("子域名地址不能重复(全局范围)!");
            }
        }
        cityCompanyService.saveCompany(cityCompany);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @ApiOperation("更新城市")
    public ResponseEntity<Object> update(@Validated @RequestBody SaveCityCompanyDto cityCompanyDto){
        CityCompany cityCompany = new CityCompany();
        BeanUtils.copyProperties(cityCompanyDto, cityCompany);
        if (StringUtils.isNotBlank(cityCompany.getSubWebDomain())){
            Boolean existed = cityCompanyService.subWebDomainIsOnly(cityCompany);
            if (existed){
                throw new BusinessException("子域名地址不能重复(全局范围)!");
            }
        }
        cityCompany.setInitFlag(org.apache.commons.lang.StringUtils.equals(cityCompanyDto.getInitFlagStr(), "是"));
        cityCompanyService.saveCompany(cityCompany);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除城市")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody List<String> ids){
        if(!CollectionUtils.isEmpty(ids)){
            cityCompanyService.deleteById(ids.get(0));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("公司经纪人规则列表")
    @PostMapping(value = "/getEmpShowList")
    public ResponseEntity<List<CompanyEmpShowDto>> getEmpShowList() {
        String corporationCode = SecurityUtils.getCorporationCode();
        Corporation corporation = corporationService.getCorporationByCorporationCode(corporationCode);
        if (corporation == null) {
            throw new BusinessException("未找到对应的集团信息");
        }
        if (corporation.getV20Flag()) {
            return ResponseEntity.ok(cityCompanyService.getV20EmpShowList(corporationCode, userService.getCityCodeList(SecurityUtils.getCurrentUsername())));
        } else {
            return ResponseEntity.ok(cityCompanyService.getEmpShowList(corporationCode, userService.getCityCodeList(SecurityUtils.getCurrentUsername())));
        }
    }

    @ApiOperation("获取经纪人展示规则")
    @PostMapping(value = "/getEmpShowRules")
    public ResponseEntity<CompanyEmpRulesDto> getEmpShowRules(@Validated @RequestBody EmpShowQueryDto empShowQueryDto) {
        return ResponseEntity.ok(cityCompanyService.getEmpShowRules(empShowQueryDto.getCompanyUuid(), SecurityUtils.getCorporationCode()));
    }

    @ApiOperation("保存经纪人展示规则")
    @PostMapping(value = "/saveEmpShowRules")
    public ResponseEntity<Object> saveEmpShowRules(@Validated @RequestBody CompanyEmpRulesDto rulesDto) {
        cityCompanyService.saveEmpShowRules(rulesDto);
        return ResponseEntity.ok(HttpStatus.OK);
    }

    @ApiOperation("开通城市库城市下拉列表")
    @PostMapping(value = "/listCityNameAndCode")
    public ResponseEntity<Object> listCityNameAndCode() {
        return ResponseEntity.ok(cityCompanyService.listCityCodeAndName());
    }


}
